package com.zuimei.zuimei.fragments;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.activities.CategoryCotentActivity;
import com.zuimei.zuimei.activities.CategoryListActivity;
import com.zuimei.zuimei.activities.Item1DescActivity;
import com.zuimei.zuimei.adapters.CategoryAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.CategoryBean;
import com.zuimei.zuimei.beans.CategoryFormatBean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.Bind;

/**
 * 精选分类
 */

public class CategoryFragment extends BaseFragment {
    @Bind(R.id.category_recycleView)
    public RecyclerView recyclerView;
    private FragmentManager fragmentManager;
    private android.support.v4.app.FragmentTransaction fragmentTransaction;
    private List<CategoryBean> list;
    private CategoryAdapter categoryAdapter;
    private List<CategoryFormatBean> list2=new ArrayList<CategoryFormatBean>();
    @Override
    public int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    public void init() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        recyclerView.setLayoutManager(linearLayoutManager);
        fragmentManager=getFragmentManager();
    }

    @Override
    public void loadData() {
        new HttpHelper(mActivity).getData(ConstantUtil.JINGXUAN_URL, new HttpHelper.OnLoadCompletedListener() {
            @Override
            public void onSuccess(String result) {
               list= ParseFindAppJson.getCategoryList(result);
                for (int i = 0; i <list.size(); i++) {

                    list2.add(new CategoryFormatBean(list.get(i).getTitle(),"",list.get(i).getIcon_url(),list.get(i).getId(),1));
                    for(int j=0;j<list.get(i).getTop_apps().size();j++){
                        list2.add(new CategoryFormatBean(list.get(i).getTop_apps().get(j).getTitle(),list.get(i).getTop_apps().get(j).getSub_title(),list.get(i).getTop_apps().get(j).getIcon_url(),list.get(i).getTop_apps().get(j).getId(),2));
                    }
                }
                categoryAdapter=new CategoryAdapter(mActivity, list2 );
                recyclerView.setAdapter(categoryAdapter);
                categoryAdapter.setOnCategoryItemClicklistener(new CategoryAdapter.OnCategoryItemClicklistener() {
                    @Override
                    public void onClick(View itemView, int position) {
                        Log.d("print", "onClick: "+"tt");
                        if(list2.get(position).getType()==1) {
                            Intent intent=new Intent();
                            intent.setClass(mActivity,CategoryListActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("id",list2.get(position).getId());
                            intent.putExtras(bundle);
                            startActivity(intent);



                        }else if(list2.get(position).getType()==2){
                            Intent intent=new Intent(mActivity, CategoryCotentActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putInt("id",list2.get(position).getId());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onLongClick(View itemView, int position) {

                    }
                });
            }

            @Override
            public void onFailure() {

            }
        });
    }


}
